package com.example.demo.huawei;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

/**
 * 十个数 任选 5个  ，
 * 计算出 两边之和 sum1 sum2
 * 求sum1 sum2最小值
 * <p>
 * 组合问题
 */
public class Main50 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] scores = new int[10];
        for (int i = 0; i < 10; i++) {
            scores[i] = sc.nextInt();
        }

        //        System.out.println(Arrays.toString(scores));
        // 总和
        int sum = Arrays.stream(scores).sum();
        // sum1 + sum2  = sum
        // sum1-(sum-sum1) = sum1-sum+sum1=2*sum1-sum 差
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> res2 = new ArrayList<>();
        dfs(0, sum, scores, new LinkedList<>(), res, res2);
        //        System.out.println(res.size());
        Collections.sort(res2);
        System.out.println(res2.get(0));

    }

    // 1 2 3 4 5 6 7 8 9 10
    private static void dfs(int start, int sum, int[] scores, LinkedList<Integer> stack, List<List<Integer>> res, List<Integer> res2) {
        Arrays.sort(scores);
        if (stack.size() == 5) {
            //            stack.stream()
            //            System.out.println(2*sum1-sum);
            res.add(new ArrayList<>(stack));
            res2.add(Math.abs(2 * (stack.get(0) + stack.get(1) + stack.get(2) + stack.get(3) + stack.get(4)) - sum));
            return;
        }

        for (int i = start; i < scores.length; i++) {
            stack.push(scores[i]);
//            System.out.println("  递归之前 => " + stack);
            dfs(i + 1, sum, scores, stack, res, res2);
            stack.pop();
//            System.out.println("  递归之前 => " + stack);
        }
    }
}
